'''
Purpose: To create Matrix differences

Usage:    outputMatrix = tdiff(inputMatrix, k)
          where: k = lagged difference order  

Created on Aug 10, 2010
@author: Peter Harrington
peter.b.harrington@gmail.com
'''
from numpy import *
def tdiff(inputMatrix, k):
    numObs,numVar = inputMatrix.shape
    if k==0:
        dmat = inputMatrix
    elif k == 1:
        dmat = matrix(zeros((numObs,numVar)))
        dmat[1:numObs,:] = inputMatrix[1:numObs,:]-inputMatrix[0:numObs-1,:]
    else:
        dmat = matrix(zeros((numObs,numVar)))
        dmat[k:numObs,:] = inputMatrix[k:numObs,:]-inputMatrix[0:numObs-k,:]
    return dmat